package com.zhilingshenghuo.app.zhly.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import com.zhilingshenghuo.app.zhly.socket.event.AppResumeEvent;
import com.zhilingshenghuo.app.zhly.socket.event.AppStopEvent;

import org.greenrobot.eventbus.EventBus;

public class MyLifecycleHandler implements Application.ActivityLifecycleCallbacks {
    private int count = 0;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (count == 0) { //后台切换到前台
            EventBus.getDefault().post(new AppResumeEvent(true));
            Log.v("MyLifecycleHandler", ">>>>>>>>>>>>>>>>>>>App切到前台");
        }
        count++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        count--;
        if (count == 0) { //前台切换到后台
            if (Utils.isLogin()){
                AppManager.getAppManager().finishActivity();
            }
            EventBus.getDefault().post(new AppStopEvent(true));
            Log.v("danxx", ">>>>>>>>>>>>>>>>>>>App切到后台");
        }
    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }
}
